////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 1    Version 10.0 (Current View)1.0         6/1/2011 2:10:38 PM    Scott
//      Frees     
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import javax.servlet.http.HttpServletRequest;

import com.intelliquip.core.UserSettings;
import com.intelliquip.hydraulicselection.UserPreferences;
import com.intelliquip.hydraulicselection.productline.web.ProductLineWebImpl;
import com.intelliquip.labels.PageLanguageUI;
import com.intelliquip.serviceclient.ItemClient;

public abstract class RedirectApp {

	protected final HttpServletRequest request;
	private final String serverName;
	private final int currentScreen;

	protected final UserPreferences userPreferences;
	protected final ProductLineWebImpl productLineWeb;
	protected final UserSettings userSettings;
	protected final PageLanguageUI pageLabels;
	protected final String targetPath;
	
	protected abstract void searchAndValidate() throws RedirectValidationException;


	public RedirectApp(RedirectParams parameterObject) {
		this.request = parameterObject.request;
		this.userPreferences = parameterObject.userPreferences;
		this.productLineWeb = parameterObject.productLineWeb;
		this.pageLabels = parameterObject.pageLabels;
		this.serverName = parameterObject.serverName;
		this.targetPath = parameterObject.targetPath;
		this.currentScreen = parameterObject.currentScreen;
		userSettings = parameterObject.userPreferences.getUserSettings();
	}

	public String prepareForRedirect() throws RedirectValidationException {

		searchAndValidate();
		String configTargetServlet = request.getParameter("ConfiguratorTab");
		validateTargetServlet(configTargetServlet);
		
		validateApp();  // hook for external app validation (i.e. config app directory for Configurator)
		int itemTempId = saveItemTemp();
		validateItemTempSave(itemTempId);
		cacheUserSettings();

		// varies between config and xsheets
		String targetPage = buildRedirectUrl(itemTempId, configTargetServlet);

		return targetPage;
	}
	
	
	protected abstract void validateApp() throws RedirectValidationException ;
	protected abstract String buildRedirectUrl(int itemTempId, String configTargetServlet);
	


	String getFullRejectMessage() {
		return pageLabels.getLabel("selectionNotSuitable");
	}

	private void validateTargetServlet(String configTargetServlet) throws RedirectValidationException {
		if(configTargetServlet == null) {
			throw new RedirectValidationException("ERROR: ConfiguratorTab parameter undefined.  Please contact Intelliquip.", getCurrentScreenUrl());
		}
	}

	
	private void cacheUserSettings() throws RedirectValidationException {
		try {
			SaveSettingsRedirectFilterSelector.cacheUserSettings(request.getSession());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RedirectValidationException("ERROR: exception while storing user settings.  Please contact Intelliquip.", getCurrentScreenUrl());
		}
	}

	private void validateItemTempSave(int itemTempId) throws RedirectValidationException {
		if (itemTempId == 0) {
			String label = pageLabels.getLabel("selectionCNBTemporarilySaved");
			throw new RedirectValidationException(label, getCurrentScreenUrl());
		}
	}

	String getCurrentScreenUrl() {
		return serverName + targetPath + ScreenNames.getURLByScreenID(currentScreen);
	}

	/**
	 * @return the itemTemp id or 0 upon error
	 */
	private int saveItemTemp() {
		String xmlString = userPreferences.writeItemXMLString(false, !userPreferences.isQuoteManagerAccessPrivileged(), true);
		int id = 0;
		try {
			id = ItemClient.saveItemTempWithIQX(xmlString, this.userPreferences.getIqxXml());
		} catch(Exception e) {
			System.err.println("UserPreferences:getConfigEntryURL-E, Error: " + e);
			e.printStackTrace();
		}
		return id;
	}

	

}
